package cn.ctodb.divination.service;

import cn.ctodb.divination.domain.Bihua;
import cn.ctodb.divination.repository.BihuaRepository;
import cn.ctodb.divination.repository.search.BihuaSearchRepository;
import cn.ctodb.divination.service.dto.BihuaDTO;
import cn.ctodb.divination.service.mapper.BihuaMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing Bihua.
 */
@Service
@Transactional
public class BihuaService {

    private final Logger log = LoggerFactory.getLogger(BihuaService.class);

    private final BihuaRepository bihuaRepository;

    private final BihuaMapper bihuaMapper;

    private final BihuaSearchRepository bihuaSearchRepository;

    public BihuaService(BihuaRepository bihuaRepository, BihuaMapper bihuaMapper, BihuaSearchRepository bihuaSearchRepository) {
        this.bihuaRepository = bihuaRepository;
        this.bihuaMapper = bihuaMapper;
        this.bihuaSearchRepository = bihuaSearchRepository;
    }

    /**
     * Save a bihua.
     *
     * @param bihuaDTO the entity to save
     * @return the persisted entity
     */
    public BihuaDTO save(BihuaDTO bihuaDTO) {
        log.debug("Request to save Bihua : {}", bihuaDTO);
        Bihua bihua = bihuaMapper.bihuaDTOToBihua(bihuaDTO);
        bihua = bihuaRepository.save(bihua);
        BihuaDTO result = bihuaMapper.bihuaToBihuaDTO(bihua);
        bihuaSearchRepository.save(bihua);
        return result;
    }

    /**
     * Get all the bihuas.
     *
     * @return the list of entities
     */
    @Transactional(readOnly = true)
    public List<BihuaDTO> findAll() {
        log.debug("Request to get all Bihuas");
        List<BihuaDTO> result = bihuaRepository.findAll().stream()
            .map(bihuaMapper::bihuaToBihuaDTO)
            .collect(Collectors.toCollection(LinkedList::new));

        return result;
    }

    /**
     * Get one bihua by id.
     *
     * @param id the id of the entity
     * @return the entity
     */
    @Transactional(readOnly = true)
    public BihuaDTO findOne(Long id) {
        log.debug("Request to get Bihua : {}", id);
        Bihua bihua = bihuaRepository.findOne(id);
        BihuaDTO bihuaDTO = bihuaMapper.bihuaToBihuaDTO(bihua);
        return bihuaDTO;
    }

    /**
     * Delete the  bihua by id.
     *
     * @param id the id of the entity
     */
    public void delete(Long id) {
        log.debug("Request to delete Bihua : {}", id);
        bihuaRepository.delete(id);
        bihuaSearchRepository.delete(id);
    }

    /**
     * Search for the bihua corresponding to the query.
     *
     * @param query the query of the search
     * @return the list of entities
     */
    @Transactional(readOnly = true)
    public List<BihuaDTO> search(String query) {
        log.debug("Request to search Bihuas for query {}", query);
        return StreamSupport
            .stream(bihuaSearchRepository.search(queryStringQuery(query)).spliterator(), false)
            .map(bihuaMapper::bihuaToBihuaDTO)
            .collect(Collectors.toList());
    }

    @Transactional(readOnly = true)
    public int getNum(char c) {
        Bihua bihua = bihuaRepository.findOneByHanziLike("%" + c + "%");
        return bihua.getNum();
    }
}
